var _partObjId;
var _actionType;
function queryOfferSpecByName(){

	$('#OfferSpecInfo').datagrid('load',getQueryParam());

	var p = $('#OfferSpecInfo').datagrid('getPager');
	$(p).pagination({
		pageSize: 10,//每页显示的记录条数，默认为10
		pageList: [5,10,15],//可以设置每页记录条数的列表
	 	beforePageText: '第',//页数文本框前显示的汉字
	   	afterPageText: '页    共 {pages} 页',
	   	displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	});
}



function saveOfferSpec(){
	var a = $('#OfferSpecInfo').datagrid('getSelections');
	if(a.length == 0){
		alert("请选择一个销售品！");
		return;
	}
	$("#p_effectObj_txt").val(a[0].NAME);
	$("#p_effectObj_txt").attr("OSPEC_ID",a[0].OFFER_SPEC_ID);
	$("#p_effectObj_txt").attr("ROLENAME",a[0].ROLENAME);
	$("#p_effectObj_txt").attr("ROLECD",a[0].ROLECD);
	$("#p_effectObj_txt").attr("OBJ_TYPE",OBJECT.OBJTYPE.OFFER_SPEC);


	$("#p_effectObj_role").empty();
	var appendOption = $("<option>");
	appendOption.val(a[0].ROLECD);
	appendOption.html(a[0].ROLENAME);
	$("#p_effectObj_role").append(appendOption);

	$("#p_effectObj_role").val(a[0].ROLECD);

	cancelOfferSpec();
}

function cancelOfferSpec(){
	$('#selectOfferSpec').window('close');
}


function getOfferSpecIds() {
	var partyEffectObj = $("span[objId=-1]");
	var tempObjId = [];
	var itemId = partyEffectObj.parents("tr").attr("id");
	var trs = "#pageBody tr[id^='"+itemId.substring(0,itemId.lastIndexOf("_"))+"_']";
	$(trs).each(function() {
		var effectobjid = $($(this).next().find("td")[1]).find("span").attr("effectobjid");
		if (effectobjid) {
			if (pack.getPackObjMap()) {
				var obj = pack.getPackObjMap().get(effectobjid);
				if (obj) {
					tempObjId.push(obj.getObjId());
				}

			}

		}
	});
	var offerSpecIds = tempObjId.join(",");
	return offerSpecIds;
}

function getQueryParam() {
	//加入动作时，带入客户订购的销售品
	var offerSpecIds = "";
	if (_actionType == ACTION.TYPE.J) {
		offerSpecIds = getOfferSpecIds();
	}

	return {
			offerSpecName: encodeURI($("#pack_offerSpecName").val()),
			partObjId: _partObjId,
			actionType: _actionType,
			offerSpecIds: offerSpecIds
		};
}
function initOfferSpec(partObjId,actionType){
	//绑定事件
	bindSpecEvent();

	_partObjId = partObjId;
	_actionType = actionType;
	$('#OfferSpecInfo').datagrid({
		url:'../util/queryOfferSpec.action',
		title: '查询结果',
		width: 620,
		height: 270,
		fitColumns: true,
		nowrap:false,
		rownumbers:true,
		showFooter:true,
		striped:true,
		pageSize: 10,//每页显示的记录条数，默认为10
		pageList: [5,10,15],//可以设置每页记录条数的列表
		idField:'OFFER_SPEC_ID',
		singleSelect:true,
		queryParams: getQueryParam(),
		columns:[[
			{field:'OFFER_SPEC_ID',title:'销售品ID',width:145,align:'center'},
			{field:'OFFER_TYPE_CD',title:'销售品类型',width:120,align:'center'},
			{field:'FEE_TYPE',title:'预后',width:80,align:'center'},
			{field:'NAME',title:'销售品规格名称',width:300,align:'center'},
			{field:'ROLENAME',title:'角色名称',width:300,align:'center'}
		]],

		onDblClickRow : function(rowIndex,rowData){
					saveOfferSpec();
				},
		pagination:true
	});


//	queryOfferSpecByName();

	var p = $('#OfferSpecInfo').datagrid('getPager');
	$(p).pagination({
		pageSize: 10,//每页显示的记录条数，默认为10
		pageList: [5,10,15],//可以设置每页记录条数的列表
	 	beforePageText: '第',//页数文本框前显示的汉字
	   	afterPageText: '页    共 {pages} 页',
	   	displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'

	});
}


function bindSpecEvent() {
	//绑定事件
	$("#pack_offerSpecName").keydown(function(e){
		if(e.keyCode == 13){
		   queryOfferSpecByName();
		}
	});
}

function initSelectLabel(){

	$('#cqrGridLabel').datagrid({
		title: '查询结果',
	//	width: 600,
		height: 270,
		url:'../util/queryPromoLabel.action',
		fitColumns: true,
		nowrap:false,
		rownumbers:true,
		showFooter:true,
		striped:true,
		pageSize: 5,//每页显示的记录条数，默认为10
		pageList: [5,10,15],//可以设置每页记录条数的列表
		idField:'labelId',
		singleSelect:true,
		columns:[[
			//{field:'ck',checkbox:true},
			{field:'labelId',title:'标签Id',width:200,align:'center'},
			{field:'labelName',title:'标签名称',width:200,align:'center'	 },
			{field:'startDate',title:'开始时间',width:200,align:'center'	 },
			{field:'endDate',title:'结束时间',width:200,align:'center'	 }
		]],
		onSelect:function(index, row){
		},
		pagination:true
	});

	var p = $('#cqrGridLabel').datagrid('getPager');

	$(p).pagination({
	 	beforePageText: '第',//页数文本框前显示的汉字
	   	afterPageText: '页    共 {pages} 页',
	   	displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	});
}
/**
 * 查询销售品标签
 */
function queryConditionsLabel(){
	$('#cqrGridLabel').datagrid('unselectAll');
	var params = {};
	params.labelName = encodeURI($("#labelName").val());
	$('#cqrGridLabel').datagrid({url:'../util/queryPromoLabel.action',
		queryParams:params});
	var p = $('#cqrGridLabel').datagrid('getPager');
	$(p).pagination({
		pageSize: 5,//每页显示的记录条数，默认为10
		pageList: [5,10,15],//可以设置每页记录条数的列表
	 	beforePageText: '第',//页数文本框前显示的汉字
	   	afterPageText: '页    共 {pages} 页',
	   	displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	});
}
/**
 * 保存销售品标签关系
 */
function saveConditionLabel(){
	var a = $('#cqrGridLabel').datagrid('getSelections');

	if(a.length == 0){
		alert("请选择一个标签");
		return;
	}
	if(a.length>1){
		alert("只能选择一个标签，请去除多余的标签！！");
		return;
	}
	$("#p_effectObj_txt").val(a[0].labelName);
	$("#p_effectObj_txt").attr("OSPEC_ID",a[0].labelId);
	$("#p_effectObj_txt").attr("OBJ_TYPE",OBJECT.OBJTYPE.LABEL);
//	$("#p_effectObj_txt").attr("ROLENAME",a[0].ROLENAME);
//	$("#p_effectObj_txt").attr("ROLECD",a[0].ROLECD);

	$('#selectOfferSpec').window('close');
}
/**
 * 取消
 */
function cancleLabel(){

	$('#selectOfferSpec').window('close');
}
